Skip to main content

JavaScript socketClient

socketClient establishes a live subscription for a provider, dataset, asset or company, and event type.

import { socketClient } from '@corva/ui/clients';

Subscribe

const subscription = {
provider: 'corva',
dataset: 'wits',
assetId: 12345,
};

const unsubscribe = socketClient.subscribe(subscription, {
onDataReceive: event => console.log(event.data),
});

The return value is a function that stops the subscription.

Subscription parameters

NameTypeRequirementDescription
providerStringRequiredDataset provider.
datasetStringRequiredDataset name.
assetIdIntegerRequired for time/depth datasetsOperational asset associated with the records.
companyIdIntegerRequired for reference datasetsCompany associated with the records.
eventStringOptionalEmpty for create events, update, or destroy.

React cleanup

import { useEffect, useRef } from 'react';
import { socketClient } from '@corva/ui/clients';

function LiveData({ assetId, onRecords }) {
const onRecordsRef = useRef(onRecords);

useEffect(() => {
onRecordsRef.current = onRecords;
}, [onRecords]);

useEffect(() => {
const unsubscribe = socketClient.subscribe(
{ provider: 'corva', dataset: 'wits', assetId },
{ onDataReceive: event => onRecordsRef.current(event.data) }
);

return unsubscribe;
}, [assetId]);

return null;
}

Always unsubscribe when a component unmounts or its subscription identifiers change. Keeping the latest callback in a ref prevents callback identity changes from unnecessarily restarting the subscription.

When to use it

Use socketClient when newly arriving records must appear without a page refresh—for example, a live drilling chart or completion table. For slowly changing data, an ordinary request on page load or user refresh may be simpler.

For the complete initial-load-plus-subscription pattern, see Receive Real-Time Updates.